package com.example.zhengchaojun.arcgis.test;

import android.support.v7.app.AppCompatActivity;

import com.esri.arcgisruntime.arcgisservices.LevelOfDetail;
import com.esri.arcgisruntime.arcgisservices.TileInfo;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.WebTiledLayer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类描述：二三维天地图基类
 * 作者：郑朝军 on 2018/5/30
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/30
 * 修改备注：
 */
public class BaseTianMapActivity extends AppCompatActivity
{
    protected WebTiledLayer CreateTianDiTuTiledLayer()
    {
        int DPI = 96;
        int minZoomLevel = 0;
        int maxZoomLevel = 17;
        int tileWidth = 256;
        int tileHeight = 256;
        List<String> SubDomain = Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});
        String mainUrl = "http://{subDomain}.tianditu.com/DataServer?T=vec_c&x={col}&y={row}&l={level}";
//        String mainUrl = "http://{subDomain}.tianditu.com/DataServer?T=img_c&x={col}&y={row}&l={level}";
        WebTiledLayer webTiledLayer = null;
        double X_MIN_2000 = -22041257.773878;
        double Y_MIN_2000 = -32673939.6727517;
        double X_MAX_2000 = 22041257.773878;
        double Y_MAX_2000 = 20851350.0432886;
        SpatialReference SRID_2000 = SpatialReference.create(3857);
        Envelope ENVELOPE_2000 = new Envelope(X_MIN_2000, Y_MIN_2000, X_MAX_2000, Y_MAX_2000, SRID_2000);
        double[] RESOLUTIONS_2000 = {
                156543.03392800014, 78271.516963999937,
                39135.758482000092, 19567.879240999919,
                9783.9396204999593, 4891.9698102499797,
                2445.9849051249898, 1222.9924525624949,
                611.49622628138, 305.748113140558,
                152.874056570411, 76.4370282850732,
                38.2185141425366, 19.1092570712683,
                9.55462853563415, 4.7773142679493699,
                2.3886571339746849, 1.1943285668550503,
                0.59716428355981721, 0.29858214164761665};
        double[] SCALES = {
                591657527.591555, 295828763.79577702,
                147914381.89788899, 73957190.948944002,
                36978595.474472001, 18489297.737236001,
                9244648.8686180003, 4622324.4343090001,
                2311162.217155, 1155581.108577,
                577790.554289, 288895.277144,
                144447.638572, 72223.819286,
                36111.909643, 18055.954822,
                9027.9774109999998, 4513.9887049999998,
                2256.994353, 1128.4971760000001
        };
        Point ORIGIN_2000 = new Point(-20037508.342787, 20037508.342787, SRID_2000);

        String mainName = "vec";
        TileInfo mainTileInfo = null;
        Envelope mainEnvelope = null;
        boolean mainIs2000 = true;

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<LevelOfDetail>();
        Point mainOrigin = null;
        for (int i = minZoomLevel; i <= maxZoomLevel; i++)
        {
            LevelOfDetail item = new LevelOfDetail(i, RESOLUTIONS_2000[i], SCALES[i]);
            mainLevelOfDetail.add(item);
        }
        mainEnvelope = ENVELOPE_2000;
        mainOrigin = ORIGIN_2000;

        mainTileInfo = new TileInfo(
                DPI,
                TileInfo.ImageFormat.PNG24,
                mainLevelOfDetail,
                mainOrigin,
                mainOrigin.getSpatialReference(),
                tileHeight,
                tileWidth
        );

        webTiledLayer = new WebTiledLayer(
                mainUrl,
                SubDomain,
                mainTileInfo,
                mainEnvelope);
        webTiledLayer.setName(mainName);
        webTiledLayer.loadAsync();
        return webTiledLayer;
    }

    protected WebTiledLayer CreateTianDiTuTiledLayer1()
    {
        int DPI = 96;
        int minZoomLevel = 0;
        int maxZoomLevel = 17;
        int tileWidth = 256;
        int tileHeight = 256;
        List<String> SubDomain = Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});
        String mainUrl = "http://{subDomain}.tianditu.com/DataServer?T=img_c&x={col}&y={row}&l={level}";
        WebTiledLayer webTiledLayer = null;
        double X_MIN_2000 = -22041257.773878;
        double Y_MIN_2000 = -32673939.6727517;
        double X_MAX_2000 = 22041257.773878;
        double Y_MAX_2000 = 20851350.0432886;
        SpatialReference SRID_2000 = SpatialReference.create(3857);
        Envelope ENVELOPE_2000 = new Envelope(X_MIN_2000, Y_MIN_2000, X_MAX_2000, Y_MAX_2000, SRID_2000);
        double[] RESOLUTIONS_2000 = {
                156543.03392800014, 78271.516963999937,
                39135.758482000092, 19567.879240999919,
                9783.9396204999593, 4891.9698102499797,
                2445.9849051249898, 1222.9924525624949,
                611.49622628138, 305.748113140558,
                152.874056570411, 76.4370282850732,
                38.2185141425366, 19.1092570712683,
                9.55462853563415, 4.7773142679493699,
                2.3886571339746849, 1.1943285668550503,
                0.59716428355981721, 0.29858214164761665};
        double[] SCALES = {
                591657527.591555, 295828763.79577702,
                147914381.89788899, 73957190.948944002,
                36978595.474472001, 18489297.737236001,
                9244648.8686180003, 4622324.4343090001,
                2311162.217155, 1155581.108577,
                577790.554289, 288895.277144,
                144447.638572, 72223.819286,
                36111.909643, 18055.954822,
                9027.9774109999998, 4513.9887049999998,
                2256.994353, 1128.4971760000001
        };
        Point ORIGIN_2000 = new Point(-20037508.342787, 20037508.342787, SRID_2000);

        String mainName = "vec";
        TileInfo mainTileInfo = null;
        Envelope mainEnvelope = null;
        boolean mainIs2000 = true;

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<LevelOfDetail>();
        Point mainOrigin = null;
        for (int i = minZoomLevel; i <= maxZoomLevel; i++)
        {
            LevelOfDetail item = new LevelOfDetail(i, RESOLUTIONS_2000[i], SCALES[i]);
            mainLevelOfDetail.add(item);
        }
        mainEnvelope = ENVELOPE_2000;
        mainOrigin = ORIGIN_2000;

        mainTileInfo = new TileInfo(
                DPI,
                TileInfo.ImageFormat.PNG24,
                mainLevelOfDetail,
                mainOrigin,
                mainOrigin.getSpatialReference(),
                tileHeight,
                tileWidth
        );

        webTiledLayer = new WebTiledLayer(
                mainUrl,
                SubDomain,
                mainTileInfo,
                mainEnvelope);
        webTiledLayer.setName(mainName);
        webTiledLayer.loadAsync();
        return webTiledLayer;
    }

    protected WebTiledLayer CreateTianDiTuTiledLayer2()
    {
        int DPI = 96;
        int minZoomLevel = 1;
        int maxZoomLevel = 18;
        int tileWidth = 256;
        int tileHeight = 256;
        List<String> SubDomain = Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});
        String mainUrl = "http://{subDomain}.tianditu.com/DataServer?T=cva_c&x={col}&y={row}&l={level}";
        WebTiledLayer webTiledLayer = null;
        double X_MIN_2000 = -180;
        double Y_MIN_2000 = -90;
        double X_MAX_2000 = 180;
        double Y_MAX_2000 = 90;
        SpatialReference SRID_2000 = SpatialReference.create(3857);
        Envelope ENVELOPE_2000 = new Envelope(X_MIN_2000, Y_MIN_2000, X_MAX_2000, Y_MAX_2000, SRID_2000);
        double[] RESOLUTIONS_2000 = {
                0.7031249999891485, 0.35156249999999994,
                0.17578124999999997, 0.08789062500000014,
                0.04394531250000007, 0.021972656250000007,
                0.01098632812500002, 0.00549316406250001,
                0.0027465820312500017, 0.0013732910156250009,
                0.000686645507812499, 0.0003433227539062495,
                0.00017166137695312503, 0.00008583068847656251,
                0.000042915344238281406, 0.000021457672119140645,
                0.000010728836059570307, 0.000005364418029785169};
        double[] SCALES = {
                2.958293554545656E8, 1.479146777272828E8,
                7.39573388636414E7, 3.69786694318207E7,
                1.848933471591035E7, 9244667.357955175,
                4622333.678977588, 2311166.839488794,
                1155583.419744397, 577791.7098721985,
                288895.85493609926, 144447.92746804963,
                72223.96373402482, 36111.98186701241,
                18055.990933506204, 9027.995466753102,
                4513.997733376551, 2256.998866688275,
                1128.4994333441375
        };
        Point ORIGIN_2000 = new Point(-180, 90, SRID_2000);

        String mainName = "cva";
        TileInfo mainTileInfo = null;
        Envelope mainEnvelope = null;
        boolean mainIs2000 = true;

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<LevelOfDetail>();
        Point mainOrigin = null;
        for (int i = minZoomLevel; i <= maxZoomLevel; i++)
        {
            LevelOfDetail item = new LevelOfDetail(i, RESOLUTIONS_2000[i - 1], SCALES[i - 1]);
            mainLevelOfDetail.add(item);
        }
        mainEnvelope = ENVELOPE_2000;
        mainOrigin = ORIGIN_2000;

        mainTileInfo = new TileInfo(
                DPI,
                TileInfo.ImageFormat.PNG24,
                mainLevelOfDetail,
                mainOrigin,
                mainOrigin.getSpatialReference(),
                tileHeight,
                tileWidth
        );

        webTiledLayer = new WebTiledLayer(
                mainUrl,
                SubDomain,
                mainTileInfo,
                mainEnvelope);
        webTiledLayer.setName(mainName);
        webTiledLayer.loadAsync();
        return webTiledLayer;
    }


    //----------------三维--------------------------------------

    protected WebTiledLayer CreateTianDiTuTiledLayer3()
    {
        int DPI = 96;
        int minZoomLevel = 1;
        int maxZoomLevel = 18;
        int tileWidth = 256;
        int tileHeight = 256;
        List<String> SubDomain = Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});
        String mainUrl = "http://{subDomain}.tianditu.com/DataServer?T=vec_w&x={col}&y={row}&l={level}";

        WebTiledLayer webTiledLayer = null;
        double X_MIN_2000 = -180;
        double Y_MIN_2000 = -90;
        double X_MAX_2000 = 180;
        double Y_MAX_2000 = 90;
        SpatialReference SRID_2000 = SpatialReference.create(3857);
        Envelope ENVELOPE_2000 = new Envelope(X_MIN_2000, Y_MIN_2000, X_MAX_2000, Y_MAX_2000, SRID_2000);
        double[] RESOLUTIONS_2000 = {
                0.7031249999891485, 0.35156249999999994,
                0.17578124999999997, 0.08789062500000014,
                0.04394531250000007, 0.021972656250000007,
                0.01098632812500002, 0.00549316406250001,
                0.0027465820312500017, 0.0013732910156250009,
                0.000686645507812499, 0.0003433227539062495,
                0.00017166137695312503, 0.00008583068847656251,
                0.000042915344238281406, 0.000021457672119140645,
                0.000010728836059570307, 0.000005364418029785169};
        double[] SCALES = {
                2.958293554545656E8, 1.479146777272828E8,
                7.39573388636414E7, 3.69786694318207E7,
                1.848933471591035E7, 9244667.357955175,
                4622333.678977588, 2311166.839488794,
                1155583.419744397, 577791.7098721985,
                288895.85493609926, 144447.92746804963,
                72223.96373402482, 36111.98186701241,
                18055.990933506204, 9027.995466753102,
                4513.997733376551, 2256.998866688275,
                1128.4994333441375
        };
        Point ORIGIN_2000 = new Point(-180, 90, SRID_2000);

        String mainName = "vec";
        TileInfo mainTileInfo = null;
        Envelope mainEnvelope = null;
        boolean mainIs2000 = true;

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<LevelOfDetail>();
        Point mainOrigin = null;
        for (int i = minZoomLevel; i <= maxZoomLevel; i++)
        {
            LevelOfDetail item = new LevelOfDetail(i, RESOLUTIONS_2000[i - 1], SCALES[i - 1]);
            mainLevelOfDetail.add(item);
        }
        mainEnvelope = ENVELOPE_2000;
        mainOrigin = ORIGIN_2000;

        mainTileInfo = new TileInfo(
                DPI,
                TileInfo.ImageFormat.PNG24,
                mainLevelOfDetail,
                mainOrigin,
                mainOrigin.getSpatialReference(),
                tileHeight,
                tileWidth
        );

        webTiledLayer = new WebTiledLayer(
                mainUrl,
                SubDomain,
                mainTileInfo,
                mainEnvelope);
        webTiledLayer.setName(mainName);
        webTiledLayer.loadAsync();
        return webTiledLayer;
    }

    protected WebTiledLayer CreateTianDiTuTiledLayer4()
    {
        int DPI = 96;
        int minZoomLevel = 1;
        int maxZoomLevel = 18;
        int tileWidth = 256;
        int tileHeight = 256;
        List<String> SubDomain = Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});
        String mainUrl =  "http://{subDomain}.tianditu.com/DataServer?T=cva_w&x={col}&y={row}&l={level}";
        WebTiledLayer webTiledLayer = null;
        double X_MIN_2000 = -180;
        double Y_MIN_2000 = -90;
        double X_MAX_2000 = 180;
        double Y_MAX_2000 = 90;
        SpatialReference SRID_2000 = SpatialReference.create(3857);
        Envelope ENVELOPE_2000 = new Envelope(X_MIN_2000, Y_MIN_2000, X_MAX_2000, Y_MAX_2000, SRID_2000);
        double[] RESOLUTIONS_2000 = {
                0.7031249999891485, 0.35156249999999994,
                0.17578124999999997, 0.08789062500000014,
                0.04394531250000007, 0.021972656250000007,
                0.01098632812500002, 0.00549316406250001,
                0.0027465820312500017, 0.0013732910156250009,
                0.000686645507812499, 0.0003433227539062495,
                0.00017166137695312503, 0.00008583068847656251,
                0.000042915344238281406, 0.000021457672119140645,
                0.000010728836059570307, 0.000005364418029785169};
        double[] SCALES = {
                2.958293554545656E8, 1.479146777272828E8,
                7.39573388636414E7, 3.69786694318207E7,
                1.848933471591035E7, 9244667.357955175,
                4622333.678977588, 2311166.839488794,
                1155583.419744397, 577791.7098721985,
                288895.85493609926, 144447.92746804963,
                72223.96373402482, 36111.98186701241,
                18055.990933506204, 9027.995466753102,
                4513.997733376551, 2256.998866688275,
                1128.4994333441375
        };
        Point ORIGIN_2000 = new Point(-180, 90, SRID_2000);

        String mainName = "cva";
        TileInfo mainTileInfo = null;
        Envelope mainEnvelope = null;
        boolean mainIs2000 = true;

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<LevelOfDetail>();
        Point mainOrigin = null;
        for (int i = minZoomLevel; i <= maxZoomLevel; i++)
        {
            LevelOfDetail item = new LevelOfDetail(i, RESOLUTIONS_2000[i - 1], SCALES[i - 1]);
            mainLevelOfDetail.add(item);
        }
        mainEnvelope = ENVELOPE_2000;
        mainOrigin = ORIGIN_2000;

        mainTileInfo = new TileInfo(
                DPI,
                TileInfo.ImageFormat.PNG24,
                mainLevelOfDetail,
                mainOrigin,
                mainOrigin.getSpatialReference(),
                tileHeight,
                tileWidth
        );

        webTiledLayer = new WebTiledLayer(
                mainUrl,
                SubDomain,
                mainTileInfo,
                mainEnvelope);
        webTiledLayer.setName(mainName);
        webTiledLayer.loadAsync();
        return webTiledLayer;
    }
}
